package com.telephone.purchase.aspect;

/**
 * @Description: 纪录日志
 * @author zhanglizhi
 * @create 2019/8/29
 */

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class WeblogAspect {

    final static Logger logger = LoggerFactory.getLogger(WeblogAspect.class);

    public WeblogAspect(){

    }

    @Pointcut("execution(public * com.telephone.purchase.controller.*.*(..))")
    private void controllerAspect(){

    }

    @Before(value = "controllerAspect()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        logger.info("[ip:{}] [url:{}] [method:({}) {}]", request.getRemoteAddr(), request.getRequestURI().toString(),
                request.getMethod(), joinPoint.getSignature());
        logger.info("args: "+Arrays.toString(joinPoint.getArgs()));
    }

}

